package com.chaofan.kings_poster.runner;

import cn.hutool.core.util.StrUtil;
import com.chaofan.kings_poster.model.Hero;
import com.chaofan.kings_poster.util.RootApis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Chaofan at 2019/1/28 13:26
 * email:chaofan2685@qq.com
 **/
@Component
public class DownloadRunner extends RootApis implements CommandLineRunner {

    @Value(value = "${download.autoBegin}")
    private Boolean autoBegin;

    @Value(value = "${download.size}")
    private String downloadSize;

    @Value(value = "${skin.downloadPash}")
    private String downloadPash;

    @Override
    public void run(String... args) throws Exception {
        if (autoBegin != null && autoBegin){
            //校验存储地址
            if (StrUtil.isEmpty(downloadPash)){
                downloadPash = getDownloadPash();
            }
            List<Hero> heroes = requestApi();
            String[] sizes = {"sProdImgNo_1","sProdImgNo_2","sProdImgNo_3","sProdImgNo_4","sProdImgNo_5","sProdImgNo_6","sProdImgNo_7","sProdImgNo_8"};
            List<String> downloadSizes = Arrays.asList(sizes);
            if (StrUtil.isEmpty(downloadSize) || !(downloadSizes.contains(downloadSize))){
                downloadSize = "sProdImgNo_";
            }
            for (Hero hero : heroes){
                downloadImg(hero.getName(),downloadSize);
            }
        }
    }


    public List<String> downloadImg(String name, String downloadSize){
        return downloadImgs(name,downloadSize);
    }
}
